package com.financing.platform.web.api;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.ManagerOrderDTO;
import com.financing.platform.dto.UserInfoDTO;
import com.financing.platform.service.IManagerOrderService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.StringUtil;
import com.financing.platform.vo.OrderVO;

@Controller("managerOrderActionApi")
@RequestMapping("/api/managerOrder")
@Scope("prototype")
public class ManagerOrderAction extends BaseAction {

	@Autowired
	private IManagerOrderService managerOrderServiceImpl;

	/**
	 * 订单权限分配
	 * @param request
	 * @param response
	 * @param managerOrder
	 * @param orderIds 
	 * @param ptype 产品类型，05私募一套逻辑，非私募另一套逻辑
	 * @return
	 */
	@RequestMapping("/addManagerOrderUser.shtml")
	@ResponseBody
	public ResultMap addManagerOrderUser(HttpServletRequest request, HttpServletResponse response, ManagerOrderDTO managerOrder,
			String orderIds, String ptype, String faids, String isConfirm) {
		try {
			if (managerOrder != null && ptype != null && !("").equals(ptype) && orderIds != null && !("").equals(orderIds)) {
				
				if(isConfirm != null && !("1").equals(isConfirm)){
					faids = faids.substring(0, faids.length() - 1);
					//处理 str 带有''
					faids=StringUtil.splitToIn(faids);
					HashMap<String, String> param=new HashMap<String, String>();
					param.put("ids", faids);
					param.put("uId",managerOrder.getSystemUserId());
					String tmpfas = "";
					// 进来的时候先判断，如果一个理财师被多个渠道管理，则给出提示，
					List<HashMap<String, Object>> list = managerOrderServiceImpl.queryFaUserBySystemUser(param);
					for(HashMap<String, Object> map : list){
						
						if((Long)map.get("ct") >0){
							tmpfas += map.get("uid") + ",";
						}
					}
					
					if(tmpfas != null && tmpfas.length() > 0){
						resultMap.setAttribute("mess", list);
						resultMap.setStatus(STATUS.FAILE);
						resultMap.setStatusCode(ResultCode.STATUS_CODE_ORDER_SYSTEM_MANAGER_MORE_FA);
						// resultMap.setMessage("理财师：" + tmpfas + "已被分配给了不同的渠道，请确认操作！");
						isConfirm = "0";
					}else {
						isConfirm = "1";
					}
				}

				if(isConfirm != null && ("1").equals(isConfirm)){
					DTOdateSet.setCreateInfo(managerOrder, request.getSession());
					DTOdateSet.setUpdateInfo(managerOrder, request.getSession());

					int count = managerOrderServiceImpl.addManagerOrderUser(managerOrder, orderIds, ptype);

					if (count > 0) {
						resultMap.setStatus(STATUS.SUCCESS);
						resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
					} else {
						resultMap.setStatus(STATUS.FAILE);
						resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
					}
				}
				
			} else {
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
			}

		} catch (Exception e) {
			log.error("addManagerOrderUser error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}
	
	@RequestMapping("/updateManagerOrderUser.shtml")
	@ResponseBody
	/**
	 * 订单权限收回
	 * @param request
	 * @param response
	 * @param managerOrder
	 * @param orderIds 
	 * @param ptype 产品类型，05私募一套逻辑，非私募另一套逻辑
	 * @return
	 */
	public ResultMap editManagerOrderUser(HttpServletRequest request, HttpServletResponse response, ManagerOrderDTO managerOrder,
			String orderIds, String ptype) {
		try {
			if (managerOrder != null && ptype != null && !("").equals(ptype) && orderIds != null && !("").equals(orderIds)) {
				DTOdateSet.setUpdateInfo(managerOrder, request.getSession());

				int count = managerOrderServiceImpl.updateManagerOrderUser(managerOrder, orderIds, ptype);

				if (count > 0) {
					resultMap.setStatus(STATUS.SUCCESS);
					resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
				} else {
					resultMap.setStatus(STATUS.FAILE);
					resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
				}

			} else {
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
			}

		} catch (Exception e) {
			log.error("addManagerOrderUser error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}
	
	/**
	 * 回收订单管理员下的所有订单
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/editManagerOrderByUser.shtml")
	@ResponseBody
	public ResultMap editManagerOrderByUser(HttpServletRequest request, HttpServletResponse response, String systemUserId, String ptype) {
		try {
			if (systemUserId != null && !("").equals(systemUserId)) {
				HttpSession session = request.getSession();
				UserInfoDTO user = (UserInfoDTO)session.getAttribute(DTOdateSet.user);
				
				int count = managerOrderServiceImpl.editManagerOrderByUser(systemUserId, user);

				if (count > 0) {
					resultMap.setStatus(STATUS.SUCCESS);
					resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
				} else {
					resultMap.setStatus(STATUS.FAILE);
					resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
				}

			} else {
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
			}

		} catch (Exception e) {
			log.error("addManagerOrderUser error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}
	
	/**
	 * 根据管理员分配订单的情况查询当前登陆人拥有哪些订单显示
	 * @param request
	 * @param response
	 * @param managerOrder
	 * @param page
	 */
	@RequestMapping("/orderFollowUp.shtml")
	@ResponseBody
	public ResultMap queryOrdersBySUid(HttpServletRequest request, HttpServletResponse response, ManagerOrderDTO managerOrder,Pager page){
		ResultMap result=new ResultMap();
		try{
			log.info("queryOrdersBySUid  begin");
			UserInfoDTO user = (UserInfoDTO) request.getSession().getAttribute(DTOdateSet.user);
			managerOrder.setSystemUserId(user.getUserId());
			
				page.setTotalCount(managerOrderServiceImpl.countOrdersBySUid(managerOrder));
				managerOrder.setBegin(page.getStartIndex());
				managerOrder.setEnd(page.getPageSize());
				
			List<OrderVO>listOrder=managerOrderServiceImpl.queryOrdersBySUid(managerOrder);
			
			result.setAttribute("listOrder",listOrder);
			result.setAttribute("page",page);
			result.setStatus(STATUS.SUCCESS);
			log.info("queryOdersBySUid end");
			
		}catch(Exception e){
			
			result.setStatus(STATUS.FAILE);
			log.error("queryOrdersBySUid error:",e);
		}
		return result;
	}

	
}
